package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.Orders;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {     //订单表

    @Autowired
    private OrdersService ordersService;


    /**
     *  用户下单  添加订单信息
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        log.info("前段传递的订单信息：{}",orders);

        //调用自定义方法
        ordersService.submit(orders);

        return R.success("下单成功！");

    }


    /**
     * 查询订单列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> list(int page, int pageSize){
        log.info("当前页：{}，每页展示数：{}",page,pageSize);

        //自定义方法
        Page<OrdersDto> ordersDtoPage = ordersService.list(page, pageSize);


        return R.success(ordersDtoPage);
    }


    /**
     * 员工  订单明细 查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime,String endTime){
        //
        log.info("页码：{}，每页条数：{}，订单号：{}，开始时间：{}，截止时间：{}",page,pageSize,number,beginTime,endTime);

        //构建分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);

        //构建条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<Orders>();
        queryWrapper.eq(number != null ,Orders::getNumber,number);           //订单号
        queryWrapper.ge(beginTime != null,Orders::getOrderTime,beginTime);   // order_time >= beginTime
        queryWrapper.le(endTime != null,Orders::getOrderTime,endTime);       // order_time <= endTime

        //SQL: SELECT * FROM orders WHERE (number = ? AND order_time >= ? AND order_time <= ?) LIMIT ?,?
        ordersService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);

    }

    /**
     * 修改 订单状态
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        //
        log.info("id:和status:{}",orders);
        System.out.println(BaseContext.getCurrentId());

        //无法获取移动端用户id

        //构建条件构造器
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getId,orders.getId());
        updateWrapper.set(Orders::getStatus,orders.getStatus());

        //SQL : update orders set status = ? where id = ?    3
        //SQL: update orders set status = ? where id = ?     4
        ordersService.update(updateWrapper);

        return R.success("修改成功！");
    }


    /**
     * 再来一单
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        //
        log.info("订单id:{}",orders);

        ordersService.again(orders);

        return R.success("下单成功！");
    }
}
